using namespace std;
void error(char *msg)
{
    perror(msg);
    exit(1);
}
void getcommands(int sock, Connections &clients)
{
   int n, teller=0;
   char buffer[256],message[256];
   while (true){
      if(teller!=0){
   	  	bzero(buffer, 256);
   	  	n = read(sock, buffer, 255);
   	  	if (n < 0) error("ERROR reading from socket");
   	  	//doorgestuurde informatie via buffer
   	  	//nu moet verwerking probleem komen, zo snel mogelijk
   	  	message[1]= ':';
   	  	message[2]= ' ';
   	  	for(int i=3; i < 256; i++){
   	  		message[i]= buffer[i-3];
   	  	}
   	  	//voor de serverlog:
   	  	cout << "Time: " << teller <<" Client " << clients.clientid() << " says: " << buffer;
   	  	string buffer2 = buffer;
   	  	string message2;
   	  	if(buffer2.substr(0,5) == "!name"){
   	  		clients.changename(buffer2.substr(6,buffer2.size()-6));
   	  		cout << "Time: " << teller << " Client " << clients.clientid() <<  " changed his name into: " << clients.whois() << endl;
   	  		n = write(sock, "Succesfully changed your name",32);
   	  	}
   	  	else if(buffer2.substr(0,5) == "!help"){
   	  		message2 = "You asked for help: \n \t 1. !name [NEW NAME] : change your name \n\t 2. !help : displays this menu\n"; 
   	  		n = write(sock, message2.c_str(),1024);
   	  	}
   	  	else{
   	  		n = write(sock, message,25);
   	  	}
   	  	if (n < 0) error("ERROR writing to socket");
   	  }
   	  else{
   	  	char * message2 = "Succesfully connected";
   	  	for(int i=0;i<21;i++){
   	  		message[i]=message2[i];
   	  	}
   	  	for(int i=22; i< 255; i++){
   	  		message[i]=0;
   	  	}
   	  	message[21] = '\n';
   	  	char buffer3[256]; 
   	  	n = read(sock, buffer3, 255);
   	  	if (n < 0) error("ERROR reading from socket");
   	  	n = write(sock, message,50);
   	  	if (n < 0) error("ERROR writing to socket");
   	  	message[0] = '0' + clients.clientid();
   	  }
   	  teller++;
   }
}

void startserver(const int portno)
{
	 Connections clients;
     int sockfd, newsockfd;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     socklen_t clilen;
     int n;
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     int pid;
     while (true) {
         newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
         if (newsockfd < 0) 
             error("ERROR on accept");
         cout << "creating connection: "<<endl;
         clients.new_client();
         pid = fork();
         //bij error
         if (pid < 0)
             error("ERROR on fork");
         if (pid == 0)  {
         	 //bij geforkt proces
             close(sockfd);
             getcommands(newsockfd, clients);
             exit(0);
         }
         else{
         	 //bij mainproces
         	 cout << "New client connected. Created pid: " <<pid;
         	 cout << ". Total clients attached to server is now: " <<  clients.numberofclients() << "."<< endl;
         	 close(newsockfd);
         }
     } /* end of while */
}
